package mia.chat.api;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

@Controller
public class BaseController {
	
	@Autowired
	public View jsonView;
	
	public static final String DATA_FIELD = "data";
	public static final String ERROR_FIELD = "error";

	public ModelAndView createErrorResponse(String sMessage) {
		return new ModelAndView(jsonView, ERROR_FIELD, sMessage);
	}
	
	@ExceptionHandler({ Exception.class, IOException.class, NullPointerException.class })
	public ModelAndView exceptionHandler(Exception e) {
		
		ModelAndView mav = new ModelAndView("error/exception");
		
		mav.addObject("showTrace", false);
		mav.addObject("exception", e);
		return mav;
	}
}
